package com.mypack.web.action;

import java.util.List;
import java.util.logging.Level;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mypack.domain.Config;
import com.mypack.domain.Email;
import com.mypack.exception.ServiceException;
import com.mypack.service.CommonService;
import com.mypack.web.DispatchAction;
import com.mypack.web.util.WebUtil;

/**
 * common action to dispatch common request from client
 * we do this can reduce the number of action class and 
 * reduce the action configuration in web.xml
 */
public class CommonAction extends DispatchAction {
	private static final long serialVersionUID = 1L;

	public void login(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String username = request.getParameter("username");
		String password = request.getParameter("password");

		if (logger.isLoggable(Level.INFO)) {
			logger.info("username = " + username);
			logger.info("password = " + password);
		}

		if (CommonService.get().login(username, password)) {
			WebUtil.setSessionAttribute(request, "user", username);
			logger.info("登录成功!");
			List<Email> emails = CommonService.get().findTop100();
			request.setAttribute("emails", emails);
			request.getRequestDispatcher("/jsp/admin/admin.jsp").forward(request, response);
		} else {
			logger.info("登录失败");
			response.sendRedirect("/jsp/login.jsp?errorcode=1");
		}
	}
	
	/**
	 * 修改系统配置, 主要是邮件方面的配置, 只能有一条配置
	 */
	public void modifyConfig(HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		Config config = WebUtil.populateBean(request, Config.class);
		CommonService.get().modifyConfig(config);
		toConfigPage(request, response);
	}	
	
	
	public void feedbackList(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<Email> emails = CommonService.get().findTop100();
		request.setAttribute("emails", emails);
		request.getRequestDispatcher("/jsp/feedbackList.jsp").forward(request, response);
	}
	
	/**
	 * 到系统配置页面
	 */
	public void toConfigPage(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Config config = CommonService.get().findConfigIf();
		request.setAttribute("config", config);
		request.getRequestDispatcher("/jsp/admin/config.jsp").forward(request, response);
	}
	
	public void feedbackAdminList(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<Email> emails = CommonService.get().findTop100();
		request.setAttribute("emails", emails);
		request.getRequestDispatcher("/jsp/admin/admin.jsp").forward(request, response);
	}
	
	/**
	 * leave a message to server, maybe you can call fadeback
	 * this method will write message to log and persistence store
	 * and email to master
	 */
	public void feedback(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String contactor = request.getParameter("contactor");
		String contactmethod = request.getParameter("contactmethod");
		String content = request.getParameter("content");
		if (logger.isLoggable(Level.INFO)) {
			logger.info("contactor = " + contactor);
			logger.info("content = " + content);
			logger.info("contactmethod = " + contactmethod);
		}
		content.replaceAll("\n", "");
		String subj = contactor + " 说 " + content + ", 联系TA " + contactmethod;
		if (logger.isLoggable(Level.INFO)) {
			logger.info("邮件主题为: " + subj);
		}
		try {
			CommonService.get().sendEmail(subj);
		} catch (ServiceException e) {
			logger.info("发送邮件失败 " + e.getMessage());
			logger.log(Level.WARNING, e.getMessage(), e);
		}
		
		Email email = new Email(contactor, contactmethod, content);
		CommonService.get().insertEmail(email);
		logger.info("保存邮件成功");
		
		response.sendRedirect("/");
	}
}
